# coding=utf-8
"""
Created on 2018年09月19日
"""
from util_base.ufo_decorators import processor
from common_base.cmd import CMD
from processor_base.base_processor import BaseProcessor
import json


@processor(CMD.CMD_ALIVECMS_READ_EFFECTS, internal_access=True)
class ReadEffects(BaseProcessor):
    """
    ReadEffects
    """
    
    TAG = 'EventCountUserScore'
    
    def __init__(self, handler):
        """
        Constructor
        """
        BaseProcessor.__init__(self, handler)

    def process(self):
        """
        process protocol
        """
        json_file_path = ''
        with open(json_file_path, 'r') as fs:
            result = json.loads(fs.read())
            library = result['library']

            group_order_num = 1

            for each_group in library:
                print('group_order_num: ' + str(group_order_num))

                group_name = each_group.get('name', '')
                group_icon_image_path = each_group.get('iconImagePath', '')

                # 插入组信息
                group_id = self.alive_cmsdb.save('effect_group', dict(name=group_name, icon_image_path=group_icon_image_path, order_num=group_order_num))
                print('group_id: ' + str(group_id))

                effects = each_group['effects']
                effect_order_num = 1
                for each_effect in effects:
                    print(str(effect_order_num))

                    effect_id = each_effect['effectId']
                    icon_image_path = each_effect.get('iconImagePath', '')
                    image_resource_name = each_effect.get('imageResourceName', '')
                    image_resource_size_desc = each_effect.get('imageResourceSizeDesc')
                    image_sequence = each_effect.get('imageSequence')
                    control_image_sequence = each_effect.get('controlImageSequence')
                    representation_index = each_effect.get('representationIndex', 0)
                    duration = each_effect.get('duration', 0)
                    min_duration = each_effect.get('minDuration', 0)
                    max_duration = each_effect.get('maxDuration', 0)
                    server_resource_path = each_effect.get('serverResourcePath')

                    # 插入贴纸信息
                    effect_info = dict(
                        group_id=group_id,
                        effect_id=effect_id,
                        icon_image_path=icon_image_path,
                        image_resource_name=image_resource_name,
                        image_resource_size_desc=json.dumps(image_resource_size_desc) if image_resource_size_desc else '',
                        image_sequence=json.dumps(image_sequence) if image_sequence else '',
                        control_image_sequence=json.dumps(control_image_sequence) if control_image_sequence else '',
                        representation_index=int(representation_index),
                        duration=float(duration),
                        min_duration=float(min_duration),
                        max_duration=float(max_duration),
                        server_resource_path=json.dumps(server_resource_path) if server_resource_path else '',
                        order_num=effect_order_num
                    )

                    self.alive_cmsdb.save('effect_info', effect_info)

                    effect_order_num += 1

                group_order_num += 1

    def _verify_params(self):
        """
        重写父类方法, 作参数校验
        """
